<template>
  <!-- // 分配角色 -->
  <div>

    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in roles" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox></el-checkbox-group>
    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getUserDetailById } from '@/api/user.js'
import { getRoles } from '@/api/setting.js'
import { assignRoles } from '@/api/employees.js'

export default {
  props: {
    curId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [],
      roles: []
    }
  },
  created() {
    this.loadRoles()
    // 根据id去发请求，拿到当前员工具备的角色
    // this.loadRolesById()
  },
  methods: {
    // 根据id去发请求，拿到当前员工具备的角色
    async loadRolesById() {
      try {
        const res = await getUserDetailById(this.curId)
        console.log('当前员工具备的角色', res)
        // 保存数据
        this.roleIds = res.data.roleIds
      } catch (err) {
        console.log('当前员工具备的角色', err)
      }
    },

    async loadRoles() {
      try {
        const res = await getRoles({ page: 1, pagesize: 10000 })
        console.log('getRoles', res)
        // 保存数据
        this.roles = res.data.rows
      } catch (err) {
        console.log('getRoles', err)
      }
    },
    closeDialog() {
      this.$emit('close')
    },
    async hSubmit() {
      try {
        // 1. 调用api
        const res = await assignRoles({ id: this.curId, roleIds: this.roleIds })
        // 成功之后
        // 提示一下
        this.$message.success('修改成功')
        // 关闭弹框
        console.log('assignRoles', res)
        this.$emit('close')
        // 保存数据
      } catch (err) {
        console.log('assignRoles', err)
      }
    }
  }
}
</script>
